package com.example.view.activity;

import android.graphics.Color;
import android.widget.FrameLayout;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.common.base.AbsActivity;
import com.example.common.utils.L;
import com.example.view.R;
import com.example.view.databinding.ActViewSlideBinding;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * android:descendantFocusability:beforeDescendants优先子类获得焦点、afterDescendants子控件不需要获得焦点时获取焦点、blockDescendants覆盖子类控件直接获得焦点
 * <p>
 * 测量：
 * view.post(runnable):此方法只调用一次，其原理是将自定义的runnable放入到消息队列的尾部，当looper调用到它时，view已经初始化完成了
 * View#onWindowFocusChanged 焦点改变监听
 * ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {}
 * 监听view树的变化，通过调用其OnGlobalLayoutListener这个接口，就可以在view树发生变化时调用，不过可能也会被调用多次
 */
@Route(path = "/view/slide")
public class SlideActivity extends AbsActivity<ActViewSlideBinding> {

    private FrameLayout fl_title;
    private int titleHeight;

    @Override
    protected int getLayoutId() {
        return R.layout.act_view_slide;
    }

    @Override
    protected void main() {
        setTitle("slide");
        fl_title = findViewById(R.id.fl_title);
        fl_title.setBackgroundColor(Color.parseColor("#ff0000"));
        fl_title.getBackground().mutate().setAlpha(0);
        fl_title.post(() -> titleHeight = fl_title.getHeight());
        List<String> data = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            data.add("item" + i);
        }
        RvAdapter adapter = new RvAdapter(data);
        mBinding.rv.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        mBinding.rv.setAdapter(adapter);
    }

    @Override
    protected void initListener() {

        mBinding.scrollView.setOnScrollViewListener((scrollX, scrollY, oldScrollX, oldScrollY) -> {

            L.e("scrollX==" + scrollX + "scrollY==" + scrollY + "oldScrollX==" + oldScrollX + "oldScrollY==" + oldScrollY);
            int scrollValue = Math.abs(oldScrollY - scrollY);
            L.e("scrollValue==" + scrollValue + "--getAlpha==" + fl_title.getBackground().getAlpha());
            if (oldScrollY - scrollY > 0) { //下拉
                if (scrollY > titleHeight) {
                    return;
                }
                int alpha = (int) (fl_title.getBackground().getAlpha() - scrollValue);
                if (alpha < 0) {
                    alpha = 0;
                }
                fl_title.getBackground().mutate().setAlpha(alpha);

            } else {//上滑
                int alpha = (int) (fl_title.getBackground().getAlpha() + scrollValue);
                if (alpha > 255) {
                    alpha = 255;
                }
                fl_title.getBackground().mutate().setAlpha(alpha);
            }

        });
    }


    private class RvAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public RvAdapter(@org.jetbrains.annotations.Nullable List<String> data) {
            super(R.layout.item_text, data);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder viewHolder, String s) {
            viewHolder.setText(R.id.tv_name, s);
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    }

}
